scripts: add an assert-rewrite diff tool - #14921
Open
RonnyPfannschmidt wants to merge 1 commit into
Open
Conversation
RonnyPfannschmidt
force-pushed
the
ronny/assert-rewrite-diff-tool
branch
from
August 22, 2026 05:53
ffea670 to
7f59292
Compare
The rewriter is read through its failure messages; what it actually
generates is invisible unless one hand-writes an ast.unparse harness.
Reviewing a change to it means asking "what does the emitted code look
like now, and how does that differ from what it was".
Add a script that answers exactly that: it dumps a snippet's rewritten
form -- as source, or as an AST -- for the source as written, for this
checkout, or for any released pytest version, and diffs two of them.
Released versions are fetched on demand via ``uv run --with``, so no
version under comparison has to be installed.
By default it diffs the snippet as written against this checkout, which
is the "show me what rewriting does here" case:
python scripts/diff-assert-rewrite.py -c 'assert (x := f()) and (x := False)'
It exits 1 when the sides differ, so it can also be used as a check.
RonnyPfannschmidt
force-pushed
the
ronny/assert-rewrite-diff-tool
branch
from
August 22, 2026 05:54
7f59292 to
be39f2f
Compare
5 tasks
nicoddemus
approved these changes
Aug 22, 2026
This was referenced Aug 22, 2026
| src = Path(__file__).parent.parent / "src" | ||
| env = os.environ | {"PYTHONPATH": str(src)} | ||
| else: | ||
| cmd = ["uv", "run", "--no-project", "--with", f"pytest=={spec}"] |
Member
There was a problem hiding this comment.
Do we want to set the expected python version as 3.14 or more ?
Member
Author
There was a problem hiding this comment.
Should probably be a cli arg
| "--right", default="worktree", metavar="SPEC", help="the same, other side" | ||
| ) | ||
| parser.add_argument("--format", choices=("source", "ast"), default="source") | ||
| parser.add_argument("--no-color", action="store_true") |
Member
There was a problem hiding this comment.
We could set the python interpreter version through uv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Split out of #14447 at @Pierre-Sassoulas' request — the tooling there was
review aid, not part of the fix, and it is useful on its own.
What it does
scripts/diff-assert-rewrite.pydumps what the rewriter generates for asnippet and diffs two sides. A side is
plain(the source as written),worktree(this checkout'ssrc/), or a released pytest version, fetched ondemand with
uv run --with pytest==VERSION— nothing under comparison has tobe installed. Output is either rewritten source (
ast.unparse) or an AST.The default —
plainagainstworktree, as source — answers @bluetech'squestion on #14447 directly, on
mainas it stands today:side_effect()appears four times where the user wrote it once — that is#14445, visible without reading the rewriter. Other modes:
It exits 1 when the sides differ, so it works as a check as well as a viewer.
Notes
Down from the three files (410 lines) that were in #14447 to one file
(130 lines): the separate dump script is gone (it existed only to be
re-invoked by the diff script, which now spawns the two workers itself), the
example_asserts.pyfixture is replaced by-c/stdin, and thecompactASTformat is folded into
ast— position attributes were never wanted.@Pierre-Sassoulas: on the primer idea — running this over a selection of open
source repos to show output changes per PR is a bigger step (corpus choice,
a stored baseline, CI plumbing) and I would rather do it separately. This is
the piece it would be built on: a stable, scriptable "what does the rewriter
emit" that already exits non-zero on a difference.
Made with Claude Code